<?
include "../db_config.php";
$lat = $HTTP_GET_VARS['lat'];
$lng = $HTTP_GET_VARS['lng'];
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>세계 최초 정화네 지하철 위치 정보 시스템</title>

<script
src="http://maps.google.com/maps?file=api&v=2&sensor=true&key=ABQIAAAAi-Dpl_qi3OFGJJzCfeNcbxSR9A0C6j8jFyzwLqb8htu1JtmEPBQAo3YZkFpuj6dhITMD9bWq-b0lXw"
type="text/javascript"></script>


<style type="text/css">
#waiting{
	position:absolute;
	top:300px;
	left:300px;
	visibility:hidden;
	background-color:white;
	z-index:100;
}
#totalNum{
	position:absolute;
	top:0px;
	left:0px;
}
#infoWin{
	position:absolute;
	top:0px;
	left:100px;
	background-color:white;
	margin:0px 10px 10px 10px;
	z-index:100;
}
#infoWin2{
	position:absolute;
	top:0px;
	left:400px;
	background-color:white;
	margin:0px 10px 10px 10px;
	z-index:100;
}

#refreshbtn{
	position:absolute;
	top:0px;
	left:500px;
	width:50px;
	height:50px;
	background-color:white;
	z-index:100;
}
.distance{
	background-color:white;
}


</style>

<script type="text/javascript">
var mapWidth = '100%';
var mapHeight = '100%';
var mapObj;
var center;
function initialize()
{
	if(GBrowserIsCompatible()){
		mapObj = new GMap2(document.getElementById("map_canvas"));
<?
if($lat != null && $lng != null)
	echo "center = new GLatLng($lat, $lng);\n";
else
	echo "center = new GLatLng(37.5225753993	,127.000369221);\n";
?>
		mapObj.setCenter(center, 13);
		//mapObj.setZoom(14);
		mapObj.setUIToDefault();

		getAPPoints();

		var i = 0;
		GEvent.addListener(mapObj, "click", getAPPoints);
		GEvent.addListener(mapObj, "dragend", getAPPoints);
		GEvent.addListener(mapObj, "zoomend", getAPPoints);

	}
	setInterval("refresh()", 1000);
}
function refresh()
{
	if(refreshState == 1)
		getAPPoints();
}



function addmarker(x, y, i, snr, viewOnPoint)
{
	var n1, n2;

	var pt_img = "http://ozs.kr/lbs/point.png";

	var base_img = "./img/";
	n1 = new GLatLng(x, y);


/*
	var marker = new GMarker(n1);
	GEvent.adListener(mapObj, "click", function()
	{
		alert('gg');
	});
	mapObj.addOverlay(marker);
	*/

	var icon = new GIcon(G_DEFAULT_ICON);
	if(i <= -10)
	{
		icon = new GIcon(G_DEFAULT_ICON);
		icon.shadow = base_img + "jungwha5.png";
		icon.shadowSize = new GSize(43, 50);
		icon.image= base_img + "stop.png";
		icon.iconSize = new GSize(40, 20);
	
	}
	else if(i < 0)
	{
		var j = -1*i;
		icon = new GIcon(G_DEFAULT_ICON);
		icon.shadow = base_img + "jungwha5.png";
		icon.shadowSize = new GSize(43, 50);
		icon.image= base_img + j + ".gif";
		icon.iconSize = new GSize(20, 20);
	}
	else
	{
		icon = new GIcon(G_DEFAULT_ICON);
		icon.image =
		"http://ozs.kr/subway/img/jungwha3.png";
		icon.iconSize = new GSize(36, 50);
	}

	markerOptions = {icon:icon};
	mapObj.addOverlay(new GMarker(n1, markerOptions));

	//mapObj.addOverlay(new GMarker(n1));
	//mapObj.openInfoWindow(n1, document.createTextNode("Hello, world"));

	//var marker = new GMarker(n1);
	//var ew = new EWindow(mapObj, E_STYLE_1);
	//ew.openOnMap(n1, 'haha');
	//ew.openOnMarker(marker, 'haha');
	//mapObj.addOverlay(marker);
	return;


	var size = new GSize(21, 18);
	var offset = new GSize(10, 18);


	var size2 = new GSize(7, 8);
	//var pt = new GMarker(n1, new GIcon(pt_img, size2));
	var ic = new GIcon(G_DEFAULT_ICON);
	ic.image = pt_img;
	ic.iconSize = size2;
	ic.iconAnchor = new GPoint(10, 18);
	ic.shadowSize = new GSize(0, 0);
	var pt = new GMarker(n1, ic);
	mapObj.addOverlay(pt);
	return;
}
function showList2(list)
{
	document.getElementById("infoWin2").innerHTML = list;
}
function showList(list)
{
	document.getElementById("infoWin").innerHTML = list;
}
function hideList()
{
	//document.getElementById("infoWin").style.visibility = "hidden";
	document.getElementById("infoWin").innerHTML = "";
}

function getAPRawData(bssid)
{
	var xmlhttp = GXmlHttp.create();
	//xmlhttp.setType(0);
	var url = "getAPRawData.php?bssid="+bssid;
	xmlhttp.open('GET', url, true);
	xmlhttp.onreadystatechange = function()
	{
		if(xmlhttp.readyState == 4)
			showAPRawData(xmlhttp.responseText);
	}
	xmlhttp.send(null);
}
	
function showAPRawData(res)
{
	//window.console.log(str);
	//window.console.log("showAPRawData");
	eval("var s = " + res +";");
	//eval(str);
	document.getElementById('totalNum').innerHTML = "총 AP : "+s.totalNum + "개";
	mapObj.clearOverlays();

	//boundary를 구하기 위해
	var bounds = new GLatLngBounds();

	for(var i in s.Points)
	{
		var node = s.Points[i];
		var p = new GLatLng(node.lat, node.lng);
		bounds.extend(p);
		addmarker(node.lat, node.lng, 0, node.SNR+"|"+node.Sig, 1);
	}
	//var b = line.getBound();
	//mapObj.setBound(b[0], b[1], b[2], b[3]);
	mapObj.setCenter(bounds.getCenter(),mapObj.getBoundsZoomLevel(bounds));

	document.getElementById('waiting').style.visibility = "hidden";

}

function showAPPoints(res)
{
	//window.console.log("showAPPoints()");
	eval("var s = " + res +";");
	document.getElementById('totalNum').innerHTML = "총 AP : "+s.totalNum + "개";
	mapObj.clearOverlays();
	for(var i in s.Points)
	{
		var node = s.Points[i];
		//window.console.log(node);
		addmarker(node.lat, node.lng, node.remainTime, node.trainId);
	}
	document.getElementById('waiting').style.visibility = "hidden";
}
//ruler 관련 코드
var rulerStatus = 0;
//최소 AP 개수
var minAPNum = 1;

var pt1, pt2;
function changeMinAPNum()
{
	var obj = document.getElementById('minAPNum');
	minAPNum = obj.value;

	getAPPoints();
}
function getAPPoints()
{
	//window.console.log("getAPPoints");
	var b = mapObj.getBounds();
	//ruler가 동작 중일 때는 동작 안함
	if(rulerStatus != 0)
		return;
	//var lt = mapObj.fromTM128ToLatLng(new NTM128(b[0], b[1]));
	//var rb = mapObj.fromTM128ToLatLng(new NTM128(b[2], b[3]));
	var lt = b.getSouthWest();
	var rb = b.getNorthEast();
	var obj = document.getElementById('waiting');
	obj.style.visibility = "visible";
	var xmlhttp = GXmlHttp.create();
	/*
	var url = "getAPs.php?b1="+rb.lat()+"&b2="+lt.lng()+"&logSelect="+logSelect;
	url = url + "&b3="+lt.lat() +"&b4="+rb.lng();
	url = url + "&sourceNum="+minAPNum;
	*/
	var url = "estimateTrainPos.php";
	//window.console.log(url);
	xmlhttp.open('GET', url, true);
	xmlhttp.onreadystatechange = function()
	{
		if(xmlhttp.readyState == 4)
		//window.console.log(xmlhttp.responseText);
			showAPPoints(xmlhttp.responseText);
	}
	xmlhttp.send(null);
}
var logSelect = 1;
function loadLog()
{
	var obj = document.getElementById('log');
	logSelect = obj.value;
	getAPPoints();
}

var refreshState = 1;
function changeRefreshState()
{
	if(refreshState == 1)
		refreshState = 0;
	else
		refreshState = 1;
}

</script>

</head>
<body onload="initialize()" onunload="GUnload()">
<div id=waiting><img src=http://ozs.kr/map/img/wait.gif></div>
<div id="map_canvas" style="width:100%; height:800px"></div>
<div id="display"></div>
<div id="infoWin"></div>
<div id="infoWin2"></div>
<div id="minAPNumDiv">최소 AP 검출 지역 : 
<select name=minAPNum id=minAPNum onChange='changeMinAPNum()'>
<?
for($i = 1; $i < 30; $i++)
	echo "<option value=$i>$i 개</option>\n";
?>
</select>
<button id=refreshbtn onclick=changeRefreshState()>자동</button>
<!--
<button onclick=addClick()>click event등록</button>
<button onclick=removeClick()>click event제거</button>
-->
<div id=totalNum></div>
</body>
</html>

				
